package com.cskaoyan.service;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.InvocationHandler;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * @author stone
 * @date 2022/08/02 17:24
 */
@Component("orderServiceProxy")
public class OrderServiceFactoryBean implements FactoryBean<OrderService> {

    @Autowired
    OrderService target;

    @Override
    public OrderService getObject() throws Exception {
        Object proxy = Enhancer.create(target.getClass(), new InvocationHandler() {
            @Override
            public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
                if (!"method3".equals(method.getName())) {
                    return method.invoke(target, objects);
                }
                System.out.println("hello world");
                Object invoke = method.invoke(target, objects);

                return invoke;
            }
        });
        return (OrderService) proxy;
    }

    @Override
    public Class<?> getObjectType() {
        return OrderService.class;
    }
}
